package cn.nuomi.shop.api.service.impl;

import cn.nuomi.shop.api.dao.UserDao;
import cn.nuomi.shop.api.dao.impl.UserDaoImpl;
import cn.nuomi.shop.api.entity.User;
import cn.nuomi.shop.api.service.UserService;

import java.math.BigDecimal;

/**
 * @author: 82571
 * @date: 2021/4/26 14:47
 * @description: 验证用户登录
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public User checkLogin(String userName, String password) {

        User dbUser = userDao.selectByUserName(userName);

        System.out.println(dbUser);

        if (null == dbUser){
            //用户不存在，直接返回
            throw new RuntimeException("用户不存在");
        }

        if (!dbUser.getPassword().equals(password)){
            //密码错误
            throw new RuntimeException("密码错误");
        }
        //密码正确
        return dbUser;
    }

    @Override
    public User checkRegister(String userName,String password) {

        if (userName==""||password==""){
            throw new RuntimeException("用户名或密码不能为空！请重新输入");
        }

        User dbUser = userDao.selectByUserName(userName);

        if (dbUser != null){
            throw new RuntimeException("用户已存在");
        }


        dbUser = userDao.registerByUserName(userName,password);

        System.out.println(dbUser);

        return dbUser;
    }

    @Override
    public User changeBalance(String userName, BigDecimal balance) {

        int result = userDao.changeBalanceByUserName(userName,balance);

        System.out.println(result);

        return null;
    }
}
